package com.gzgs.controller;

import com.gzgs.pojo.SysFile;
import com.gzgs.service.FilePathService;
import org.aspectj.weaver.patterns.TypePatternQuestions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
public class FileController {

    @Autowired
    private FilePathService filePathService;
    // 传入的参数file是我们指定的文件

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file, Model model) {

        if (filePathService.Upload(file)){
            List<SysFile> files = filePathService.getAll();
            model.addAttribute("fileList",files);
            model.addAttribute("filemsg","上传成功");
            return "home.html";
        }else {
            List<SysFile> files = filePathService.getAll();
            model.addAttribute("fileList",files);
            model.addAttribute("filemsg","上传失败");
            return "home.html";
        }

    }
    @ResponseBody
    @RequestMapping("/download/{filename}")
    public String downFile(@PathVariable String filename,
                         HttpServletResponse response,
                         Model model) {
        System.out.println("1");
        // 得到要下载的文件名
        String fileName = filename;
        System.out.println("2");
        try {
            //fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
            System.out.println("3");
            // 获取上传文件的目录
            String sc = "G:\\upload\\";
            System.out.println("4");
            // 上传位置
            //String fileSaveRootPath = sc.getRealPath("/img");

            System.out.println(sc + fileName);
            // 得到要下载的文件
            File file = new File(sc + fileName);

            // 如果文件不存在
            if (!file.exists()) {
                model.addAttribute("filemsg", "您要下载的资源已被删除！！");
                System.out.println("您要下载的资源已被删除！！");
                List<SysFile> files = filePathService.getAll();
                model.addAttribute("fileList",files);
                return "您要下载的资源已被删除！！";
            }
            // 处理文件名
           String realname = fileName.substring(fileName.indexOf("_") + 1);
            // 设置响应头，控制浏览器下载该文件
            response.setHeader("content-disposition", "attachment;filename="
                    + URLEncoder.encode(realname, "UTF-8"));
            // 读取要下载的文件，保存到文件输入流
            FileInputStream in = new FileInputStream(sc + fileName);
            // 创建输出流

            OutputStream out = response.getOutputStream();

            // 创建缓冲区
            byte buffer[] = new byte[1024];
            int len = 0;
            // 循环将输入流中的内容读取到缓冲区当中
            while ((len = in.read(buffer)) > 0) {
                // 输出缓冲区的内容到浏览器，实现文件下载
                out.write(buffer, 0, len);
            }
            // 关闭文件输入流
            in.close();
            // 关闭输出流
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("filemsg", "下载完成！！");
        System.out.println("下载完成！！");
        List<SysFile> files = filePathService.getAll();
        model.addAttribute("fileList",files);
        return "下载完成！！";
    }
}

